/**
 * Created by yudongyang on 2016/2/29 14:19.
 * mail: yudongyang@kehaoinfo.com
 **/

package com.wujie.activies.service.helper.imp;

import com.wujie.activies.ConfigManage;
import com.wujie.activies.model.Contact;
import com.wujie.activies.service.helper.IReadMikeExcels;
import org.apache.log4j.Logger;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.jxls.reader.ReaderBuilder;
import org.jxls.reader.XLSReader;
import org.xml.sax.SAXException;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by yudongyang on 2016/2/29.
 */
public class ReadMikeExcelsImpl implements IReadMikeExcels{
    static Logger TRACE = Logger.getLogger(IReadMikeExcels.class);
    private static final String wujiexml = ConfigManage.getProp("mikeexcel_tpl").toString();
    private static final String titleregstr = "（(\\d+\\.\\d+)(.*)·(.*)\\|(.*)）";

    @Override
    public Map<String, Object> getExcelData(File excel) throws Exception {
        return readExcel(new FileInputStream(excel));
    }

    //该类因为jxls读取数据时需要一个bean，故而构造出来并且为public。
    public class TitleBean{
        private String title;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
    }

    public Map<String,Object> readExcel(InputStream inputStream) throws Exception {
        Map<String,Object> retVal = new HashMap<>(4);
        TRACE.info("Reading xml config file and constructing XLSReader: " + wujiexml);
        try (InputStream xmlInputStream = ReadMikeExcelsImpl.class.getResourceAsStream(wujiexml)) {
            XLSReader reader = ReaderBuilder.buildFromXML(xmlInputStream);
            try (InputStream xlsInputStream =inputStream) {

                List<Contact> contacts = new ArrayList<>();
                Map<String, Object> beans = new HashMap<>();
                TitleBean titleBean = new TitleBean();

                beans.put("contacts", contacts);
                beans.put("titles", titleBean);
                TRACE.info("Reading the data...");
                reader.read(xlsInputStream, beans);
                String title = titleBean.getTitle();

                Pattern p2= Pattern.compile(titleregstr);
                Matcher m2 = p2.matcher(title);
                m2.find();
                String time = m2.group(1);
                String activitytypename = m2.group(3);
                String activityname = m2.group(4);


                retVal.put("contacts",contacts);
                retVal.put("activitytype",activitytypename);
                retVal.put("activityname",activityname);
                retVal.put("time",time);

            }
        } catch (IOException |SAXException |InvalidFormatException e) {
            throw new Exception("读取excel表格出错 ",e);
        }
        return retVal;
    }

    public static void main(String[] args){
        ReadMikeExcelsImpl readMikeExcels = new ReadMikeExcelsImpl();
        String wujiedata = "/dep/tpl/wujie_data.xls";
        try {
            readMikeExcels.readExcel(ReadMikeExcelsImpl.class.getResourceAsStream(wujiedata));
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
